package com.super8.super8newtv;

import androidx.appcompat.app.AppCompatActivity;

import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;

import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.BridgeWebViewClient;
import com.super8.super8newtv.utils.LogUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MovieTVActivity extends AppCompatActivity {

    @BindView(R.id.shopping_web_view)
    BridgeWebView webView;
    private WebSettings settings;

    String imageUrl = "https://xiaobaotv.net/";

    @BindView(R.id.fl_video)
    FrameLayout mLayout;    // 用来显示视频的布局
    private View mCustomView;	//用于全屏渲染视频的View
    private WebChromeClient.CustomViewCallback mCustomViewCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_tvactivity);
    }




    /**
     * 初始化webview
     */
    private void initWebView() {
        //支持js
        settings = webView.getSettings();
        settings.setJavaScriptCanOpenWindowsAutomatically(true);//支持通過JS打開新窗口
        settings.setJavaScriptEnabled(true);//支持js

        settings.setDomStorageEnabled(true);//设置适应Html5的一些方法
        settings.setGeolocationEnabled(true);//啟用地理定位

        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);//webview中緩存设置--不缓存Don't use the cache, load from the network.
        settings.setDefaultTextEncodingName("utf-8"); //避免中文乱码

        //设置适应屏幕NORMAL
        //NARROW_COLUMNS
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setLoadWithOverviewMode(true);
//        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);  //不使用缓存
        webView.setBackgroundColor(Color.TRANSPARENT);
        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);  //硬件加速
        String userAgent = settings.getUserAgentString();
        String newuserAgent = userAgent + " oadzApp";
        settings.setUserAgentString(newuserAgent);
        webView.setWebChromeClient(new WebChromeClient(){
            /**
             * @param view
             * @param requestedOrientation
             * @param callback
             * @deprecated
             */
            @Override
            public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
                super.onShowCustomView(view, requestedOrientation, callback);

                LogUtils.e("1111","==========onShowCustomView==============");
                //如果view 已经存在，则隐藏
                if (mCustomView != null) {
                    callback.onCustomViewHidden();
                    return;
                }

                mCustomView = view;
                mCustomView.setVisibility(View.VISIBLE);
                mCustomViewCallback = callback;
                mLayout.addView(mCustomView);
                mLayout.setVisibility(View.VISIBLE);
                mLayout.bringToFront();

                //设置横屏
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }

            @Override
            public void onHideCustomView() {
                super.onHideCustomView();
                LogUtils.e("1111","==========onHideCustomView==============");
                if (mCustomView == null) {
                    return;
                }
                mCustomView.setVisibility(View.GONE);
                mLayout.removeView(mCustomView);
                mCustomView = null;
                mLayout.setVisibility(View.GONE);
                try {
                    mCustomViewCallback.onCustomViewHidden();
                } catch (Exception e) {
                }
//                titleView.setVisibility(View.VISIBLE);
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
            }
        });
        webView.setWebViewClient(new MovieTVActivity.MyWebViewClient(webView));
        if (Build.VERSION.SDK_INT >= 19) {
            settings.setLoadsImagesAutomatically(true);
        } else {
            settings.setLoadsImagesAutomatically(false);
        }


    }

    class MyWebViewClient extends BridgeWebViewClient {
        public MyWebViewClient(BridgeWebView webView) {
            super(webView);
        }

        @Override
        public void onPageStarted(WebView view, String loadUrl, Bitmap favicon) {

            super.onPageStarted(view, loadUrl, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (!webView.getSettings().getLoadsImagesAutomatically()) {
                webView.getSettings().setLoadsImagesAutomatically(true);
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
//            view.loadUrl(url);
//            return true;
            return super.shouldOverrideUrlLoading(view, url);
        }


    }


}